//index.js
//获取应用实例
const app = getApp()
var that = {}
Page({
  data: {
    username: '',
    account: '',
    password: '',
    password1: '',


    headpic: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3520513685,3451692321&fm=26&gp=0.jpg',
    objectArray: ['信息网络安全学院','治安学院','犯罪学学院','警体战训学院','侦查与反恐怖学院','刑事科学技术学院','法学院','交通管理学院','公安管理学院','国际警务执法学院'],
    index: 0,
    objectArray1: ['一区队', '二区队', '三区队', '四区队', '五区队', '六区队'],
    index1: 0,

    objectArray2: ['法学','治安学','侦查学','刑事科学技术','安全防范工程','交通管理工程','犯罪学','情报学','公安管理学','涉外警务','网络安全与执法','警务指挥与战术','公安视听技术'],
    index2: 0,

  },

  onShow: function () {
    // 生命周期函数--监听页面显示
    wx.hideTabBar({})
  },
  onLoad: function () {
    that = this;
    wx.setNavigationBarTitle({
      title: '学生注册'
    })
  },


  // 获取输入账号 
  usernameInput: function (e) {
    this.setData({
      username: e.detail.value
    })
  },
  useraccountInput: function (e) {
    this.setData({
      account: e.detail.value
    })
  },

  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  // 获取输入密码 
  passwordInput1: function (e) {
    this.setData({
      password1: e.detail.value
    })
  },


  //  点击组件确定事件  
  bindPickerChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  //  点击组件确定事件  
  bindPickerChange1: function (e) {
    console.log(e.detail.value)
    this.setData({
      index1: e.detail.value
    })
  },

  bindPickerChange2: function (e) {
    console.log(e.detail.value)
    this.setData({
      index2: e.detail.value
    })
  },
  chooseImage: function () {

    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        // console.log(res)
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths[0])
        wx.uploadFile({
          url: app.globalData.baseUrl + '/upload',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {},
          formData: {},
          success: function (res) {

            console.log(res.data)
            let temp = JSON.parse(res.data);
            var img = app.globalData.baseUrl + '/' + temp.data;
            console.log(img);
            that.setData({
              headpic: img,
            })
          },
          fail: function (res) {},
          complete: function (res) {},
        })




      }
    })
  },
  // 注册处理
  login: function () {
    var that = this;
    if (this.data.username.length == 0 || this.data.password.length == 0 || this.data.account.length == 0) {
      wx.showToast({
        title: '姓名或密码学号不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.password1 != this.data.password) {
      wx.showToast({
        title: '两次密码不一致',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.request({
      url: app.globalData.baseUrl + '/user/register', // 仅为示例，并非真实的接口地址
      method: 'post',
      data: {
        pwd: that.data.password,
        name: that.data.username,
        img: that.data.headpic,
        account: that.data.account,
        type: 0,
        xueyuan: that.data.objectArray[that.data.index],
        banji: that.data.objectArray1[that.data.index1],
        zhuanye: that.data.objectArray2[that.data.index2],
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.code == 0) {


          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })

        }
      }
    })

  }
})